/* 
 * 定义itfreer.arcgis.geometryservice处理模块
 */
(function(window, angular, undefined){
	/* 
	 * 定义ArcGIS geometry处理模块
	 */
	var itfreerarcgisGeometryserviceModule = angular.module('itfreer.arcgis.geometryservice', ['itfreer.arcgis.core']);

	/* 
	 * 定义ArcGIS geometry处理模块
	 */
	itfreerarcgisGeometryserviceModule.factory('$itfreer_arcgis_geometryservice', 
		function($rootScope, $itfreer_arcgis_core, $gisObject) {
			$itfreer_arcgis_geometryservice = {

				/* 
				 * 投影转换成功
				 */
				onProjectCompleteEventFunction : null,

				/* 
				 * 简单化成功
				 */
				onSimplifyCompleteEventFunction : null,

				/* 
				 * 长度计算成功
				 */
				onLengthsCompleteEventFunction : null,

				/* 
				 * 长度面积计算成功
				 */
				onAreasAndLengthsCompleteEventFunction : null,

				/* 
				 * 缓冲分析成功
				 */
				onBufferCompleteEventFunction : null,

				/* 
				 * 初始化
				 */
				init : function(){
					 //添加onProjectComplete完成后事件监听
				    dojo.connect($gisObject.geometryService, "onProjectComplete", $itfreer_arcgis_geometryservice.onProjectComplete);
				    dojo.connect($gisObject.geometryService, "onSimplifyComplete", $itfreer_arcgis_geometryservice.onSimplifyComplete);
				    dojo.connect($gisObject.geometryService, "onLengthsComplete", $itfreer_arcgis_geometryservice.onLengthsComplete);
			     	dojo.connect($gisObject.geometryService, "onAreasAndLengthsComplete", $itfreer_arcgis_geometryservice.onAreasAndLengthsComplete);
					dojo.connect($gisObject.geometryService, "onBufferComplete", $itfreer_arcgis_geometryservice.onBufferComplete);
				},

				/* 
				 * 投影转换成功
				 */
				onProjectComplete : function(graphics){
					if($itfreer_arcgis_geometryservice.onProjectCompleteEventFunction){
						$itfreer_arcgis_geometryservice.onProjectCompleteEventFunction(graphics);
					}
				},

				/* 
				 * 简单化成功
				 */
				onSimplifyComplete : function(graphics){
					if($itfreer_arcgis_geometryservice.onSimplifyCompleteEventFunction){
						$itfreer_arcgis_geometryservice.onSimplifyCompleteEventFunction(graphics);
					}
				},

				/* 
				 * 长度计算成功
				 */
				onLengthsComplete : function(graphics){
					if($itfreer_arcgis_geometryservice.onLengthsCompleteEventFunction){
						$itfreer_arcgis_geometryservice.onLengthsCompleteEventFunction(graphics);
					}
				},

				/* 
				 * 长度面积计算成功
				 */
				onAreasAndLengthsComplete : function(graphics){
					if($itfreer_arcgis_geometryservice.onAreasAndLengthsCompleteEventFunction){
						$itfreer_arcgis_geometryservice.onAreasAndLengthsCompleteEventFunction(graphics);
					}
				},

				/* 
				 * 缓冲分析成功
				 */
				onBufferComplete : function(graphics){
					if($itfreer_arcgis_geometryservice.onBufferCompleteEventFunction){
						$itfreer_arcgis_geometryservice.onBufferCompleteEventFunction(graphics);
					}
				},

				/* 
				 * 进行投影转换
				 */
				project : function(geometrys, outSR){
					// 进行投影转换，完成后调用projectComplete
				    var params = new esri.tasks.ProjectParameters();
					params.geometries = geometrys;
					params.outSR = outSR;
					$gisObject.geometryService.project(params);
				}, 

				/* 
				 * 长度计算
				 */
				lengths : function(geometrys){
					var lengthParams = new esri.tasks.LengthsParameters();
					lengthParams.polylines = geometrys;
					lengthParams.lengthUnit = esri.tasks.GeometryService.UNIT_KILOMETER;
			     	$gisObject.geometryService.lengths(lengthParams);
				}, 

				/* 
				 * 长度面积计算
				 */
				areasAndLengths : function(geometrys){
					var areasAndLengthParams = new esri.tasks.AreasAndLengthsParameters();
					areasAndLengthParams.areaUnit = esri.tasks.GeometryService.UNIT_SQUARE_KILOMETERS;
					areasAndLengthParams.polygons = geometrys;
					// 进行面积测量
					$gisObject.geometryService.areasAndLengths(areasAndLengthParams);
				}, 

				/* 
				 * 简单化处理
				 */
				simplify : function(geometrys){
			     	$gisObject.geometryService.simplify(geometrys);
				},

				/* 
				 * 简单化处理
				 */
				buffer : function(distances, graphics){
					var params = new esri.tasks.BufferParameters();
					params.distances = [distances];
					// 做缓存，都使用投影坐标
					params.outSpatialReference = $gisObject.tywkid;
					params.bufferSpatialReference = $gisObject.tywkid;
					params.geometries = graphics;
					//buffer的单位，米
	   				params.unit = esri.tasks.BufferParameters.UNIT_METER;
					$gisObject.geometryService.buffer(params);
				}
			};
			for(f in $itfreer_arcgis_geometryservice){
				$rootScope[f] = $itfreer_arcgis_geometryservice[f];
			}
			return $itfreer_arcgis_geometryservice;
		}
	);

	/* 
	 * 加载服务器
	 */
	itfreerarcgisGeometryserviceModule.run(function($rootScope, $itfreer_arcgis_geometryservice) {
	  	
	});
}(window, angular));